package com.lxq.smartsign.smartattendance;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lxq.smartsign.smartattendance.util.MonitorConfig;
import com.lxq.smartsign.smartattendance.websocket.WebSocketHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Hashtable;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RequestQueue queue;
    private TextView workScheduleView;
    private TextView attendanceRange;
    private int forenoonHour;
    private int forenoonMinute;
    private int afternoonHour;
    private int afternoonMinute;

    private int forenoonClockInRangeHour;
    private int forenoonClockInRangeMinute;
    private int afternoonClockInRangeHour;
    private int afternoonClockInRangeMinute;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("打卡监视器");
        queue= Volley.newRequestQueue(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏设置
        workScheduleView=findViewById(R.id.attendance_time);
        attendanceRange=findViewById(R.id.attendance_range);
        workScheduleView.setOnClickListener(this);
        getWorkSchedule();
        //WebSocketHandler.getInstance("ws://"+MonitorConfig.IP+":"+MonitorConfig.PORT+"/socket");
        try {
            genQRBitmap();
            Toast.makeText(this, "生成二维码成功", Toast.LENGTH_SHORT).show();
        } catch (WriterException | JSONException e) {
            e.printStackTrace();
            Toast.makeText(this, "生成二维码失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void getWorkSchedule(){
        String url= MonitorConfig.SERVER_ADDRESS+"/work_schedule";
        JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,url,null,
                response -> {
                    forenoonHour=response.optInt("forenoonHour");
                    forenoonMinute=response.optInt("forenoonMinute");
                    afternoonHour=response.optInt("afternoonHour");
                    afternoonMinute=response.optInt("afternoonMinute");
                    forenoonClockInRangeHour=response.optInt("forenoonClockInRangeHour");
                    forenoonClockInRangeMinute=response.optInt("forenoonClockInRangeMinute");
                    afternoonClockInRangeHour=response.optInt("afternoonClockInRangeHour");
                    afternoonClockInRangeMinute=response.optInt("afternoonClockInRangeMinute");

                    workScheduleView.setTextColor(Color.GRAY);
                    String startWork=(forenoonHour>9?forenoonHour:"0"+forenoonHour)+"："+
                            (forenoonMinute>9?forenoonMinute:"0"+forenoonMinute);
                    String endWork=(afternoonHour>9?afternoonHour:"0"+afternoonHour)+"："+
                            (afternoonMinute>9?afternoonMinute:"0"+afternoonMinute);
                    workScheduleView.setText("上班时间："+startWork+"\n下班时间："+endWork);

                    String forenoonRange=
                            (forenoonClockInRangeHour>9?forenoonClockInRangeHour:"0"+forenoonClockInRangeHour)+"："+
                            (forenoonClockInRangeMinute>9?forenoonClockInRangeMinute:"0"+forenoonClockInRangeMinute)+"——"+
                            (forenoonHour>9?forenoonHour:"0"+forenoonHour)+"："+
                            (forenoonMinute>9?forenoonMinute:"0"+forenoonMinute);
                    String afternoonRange=
                            (afternoonHour>9?afternoonHour:"0"+afternoonHour)+"："+
                            (afternoonMinute>9?afternoonMinute:"0"+afternoonMinute)+"——"+
                            (afternoonClockInRangeHour>9?afternoonClockInRangeHour:"0"+afternoonClockInRangeHour)+"："+
                            (afternoonClockInRangeMinute>9?afternoonClockInRangeMinute:"0"+afternoonClockInRangeMinute);
                    attendanceRange.setText("上班打卡时间：\n"+forenoonRange+"\n下班打卡时间：\n"+afternoonRange);

                    Toast.makeText(this, "已刷新上下班时间", Toast.LENGTH_SHORT).show();
                },error -> {
            workScheduleView.setText("获取上下班时间失败");
            workScheduleView.setTextColor(Color.RED);
        });
        queue.add(request);
    }

    private void genQRBitmap() throws WriterException, JSONException {
        int width=500;
        int height=500;

        JSONObject qrString=new JSONObject();
        qrString.put("rand",MonitorConfig.RANDOM);
        qrString.put("mac",MonitorConfig.BLUETOOTH_MAC);
        qrString.put("androidId",MonitorConfig.ANDROID_ID);

        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(qrString.toString(), BarcodeFormat.QR_CODE, width, height,hints);
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * width + x] = Color.BLACK;
                } else {
                    pixels[y * width + x] = Color.WHITE;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        ImageView qrImg=findViewById(R.id.random_img);
        qrImg.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.attendance_time:
                getWorkSchedule();
                break;
        }
    }
}
